package com.batman.baselibrary.data.result;

import android.annotation.SuppressLint;
import android.app.Application;
import android.text.TextUtils;

import com.batman.baselibrary.preference.SharedPreferencesHelper;

/**
 * 作者：yuhb on 2018/4/19 13:23.
 * 描述：
 */

public final class InitHelper {

    private SplashBean splash;
    private Application application;
    @SuppressLint("StaticFieldLeak")
    private static InitHelper instances;

    private InitHelper(Application application) {
        this.application = application;
    }

    public static void init(Application application) {
        instances = new InitHelper(application);
    }


    public synchronized static SplashBean getInit() {
        if (instances == null) {
            return new SplashBean();
        }
        if (instances.splash == null) {
            instances.splash = SharedPreferencesHelper.loadFormSource(instances.application, SplashBean.class);
        }
        if (instances.splash == null) {
            instances.splash = new SplashBean();
        }
        return instances.splash;
    }

    public static void updateInitCache(SplashBean splash) {
        if (splash == null) {
            return;
        }
        if (TextUtils.isEmpty(splash.getPicPrefix()) && instances.splash != splash) {
            splash.setPicPrefix(splash.getPicPrefix());
        }

        if (TextUtils.isEmpty(splash.getCustomerAgreement()) && instances.splash != splash) {
            splash.setCustomerAgreement(splash.getCustomerAgreement());
        }

        if (TextUtils.isEmpty(splash.getNewbieGuide()) && instances.splash != splash) {
            splash.setNewbieGuide(splash.getNewbieGuide());
        }


        if (TextUtils.isEmpty(splash.getSharePicUrl()) && instances.splash != splash) {
            splash.setSharePicUrl(splash.getSharePicUrl());
        }

        instances.splash = splash;
        SharedPreferencesHelper.save(instances.application, splash);
    }

    public static void clearUserCache() {
        instances.splash = null;
        SharedPreferencesHelper.remove(instances.application, SplashBean.class);
    }

    public static void init(SplashBean splash) {
        // 保存缓存
        updateInitCache(splash);
    }


}
